package com.aoip.telemetry.telemetry.configureui.business.query.monitory.service.impl;

import com.aoip.telemetry.telemetry.configureui.business.query.monitory.mapper.MonitoringDataQueryMapper;
import com.aoip.telemetry.telemetry.configureui.business.query.monitory.mapper.OriginalMessageQueryMapper;
import com.aoip.telemetry.telemetry.configureui.business.query.monitory.service.MonitoringDataService;
import com.aoip.telemetry.telemetry.core.redis.RedisCache;
import com.aoip.telemetry.telemetry.entity.common.OriginalMessageEntity;
import com.aoip.telemetry.telemetry.entity.common.OriginalMonitoringDataEntity;
import com.aoip.telemetry.telemetry.entity.enums.CacheKeyEnum;
import com.aoip.telemetry.telemetry.entity.equipment.StOnlineStatusEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenbai
 * @version 1.0
 * @date 2022/1/7 15:22
 */
@Service
@Slf4j
public class MonitoringDataServiceImpl implements MonitoringDataService {

    @Resource
    private OriginalMessageQueryMapper originalMessageMapper;

    @Resource
    private MonitoringDataQueryMapper monitoringDataMapper;

    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    @Resource
    private RedisCache redisCache;
    @Override
    public List<OriginalMessageEntity> list(OriginalMessageEntity entity) {
        String createTimeCn = entity.getCreateTimeCn();
        if (!StringUtils.hasText(createTimeCn)) {
            entity.setCreateTimeCn(LocalDate.now().format(DATE_FORMATTER));
        }

        List<OriginalMessageEntity> list = originalMessageMapper.list(entity);
        monitorDataList(list);
        return list;
    }


    @Override
    public List<OriginalMessageEntity> detail(OriginalMessageEntity entity) {
        QueryWrapper<OriginalMessageEntity> queryWrapper = new QueryWrapper<>();
        String stCode = entity.getStCode();
        if (!StringUtils.hasText(stCode)) {
            return null;
        }
        String beginQueryTimeCn = entity.getBeginQueryTimeCn();
        if (StringUtils.hasText(beginQueryTimeCn)) {
            queryWrapper.ge(true, "create_time", LocalDate.parse(beginQueryTimeCn, DATE_FORMATTER));
        }
        String endQueryTimeCn = entity.getEndQueryTimeCn();
        if (StringUtils.hasText(endQueryTimeCn)) {
            LocalDate end = LocalDate.parse(endQueryTimeCn, DATE_FORMATTER);
            queryWrapper.le(true, "create_time", end.plusDays(1));
        }
        queryWrapper.eq(true, "st_code", stCode);
        queryWrapper.orderByDesc("create_time");
        List<OriginalMessageEntity> list = originalMessageMapper.selectList(queryWrapper);
        monitorDataList(list);
        return list;
    }

    private void monitorDataList(List<OriginalMessageEntity> list) {
        if (list == null || list.size() < 1) {
            return;
        }
        list.forEach(item->{
            String key = CacheKeyEnum.STATION_ONLINE_STATUS + ":" + item.getStCode();
            StOnlineStatusEntity onlineStatus = redisCache.getCacheObject(key);
            item.setStOnlineStatusEntity(onlineStatus);
        });
        QueryWrapper<OriginalMonitoringDataEntity> queryDataWrapper = new QueryWrapper<>();
        List<Long> originalIds = list.stream().map(OriginalMessageEntity::getId).collect(Collectors.toList());
        queryDataWrapper.in(true, "original_message_id", originalIds);
        List<OriginalMonitoringDataEntity> monitoringDataList = monitoringDataMapper.selectList(queryDataWrapper);
        list.forEach(item -> {
            StringBuilder sb = new StringBuilder();
            for (OriginalMonitoringDataEntity monitoringData : monitoringDataList) {
                if (item.getId() == monitoringData.getOriginalMessageId()) {
                    String identifier = monitoringData.getMonitoringDataIdentifier();
                    if (identifier.contains("监测时间")) {
                        continue;
                    }
//                    int i = identifier.indexOf("水位") +
//                            identifier.indexOf("流量") +
//                            identifier.indexOf("雨量") +
//                            identifier.indexOf("流速") +
//                            identifier.indexOf("闸门")
//                            ;
//                    if (i > -5) {
                        sb.append(identifier)
                                .append(":")
                                .append(monitoringData.getMonitoringDataValue())
                                .append("  |  ");
//                    }
                }
            }
            String data = sb.toString();
            if (data.endsWith("|  ")) {
                data = data.substring(0, data.length() - 3);
            }
            item.setMonitorData(data);
        });
    }

}
